from django.shortcuts import render,redirect,HttpResponse,reverse
from django.views import View
from django.http import  JsonResponse
from django.contrib.auth import authenticate,logout,login
from .form import UserLoginForm,UserRegisterModelForm
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User

from rest_framework.views import APIView


'''
class Login(View):
    """
    用户登录
    """


    def get(self,request):
        form = UserLoginForm()
        current_path = request.get_full_path()
        print("current_path:",current_path)


        return render(request, 'myaccount/login.html',{"form":form})

    def post(self,request):
        username = request.POST.get("username")
        if not username:
            return HttpResponse("请输入正确的账号")
        form = UserLoginForm(request.POST)
        print("form:",form)
        username = request.POST.get("username")
        password = request.POST.get("password")
        print("username:",username,"password:",password)
        path = (request.get_full_path()).split('=')[-1]
        print("current_path:", path)
        if path == request.path:
            path = '/'


        flag = False
        if form.is_valid():
            user = authenticate(username=username,password=password)
            print("user:",user)
            print(form.cleaned_data)
            if user:
                # 将用户数据保存在 session 中，即实现了登录动作
                login(request,user)
                flag = True
        if flag:
            return redirect(path)
            # return HttpResponse("登录成功")
        else:
            return HttpResponse("账号或密码输入有误。请重新输入~")


    


class Register(View):
    """
    用户注册
    """

    def get(self,request):
        current_path = request.path
        print("current_path:",current_path)
        form = UserRegisterModelForm()
        return render(request,'myaccount/register.html',{"form":form})

    def post(self,request):
        form = UserRegisterModelForm(request.POST)

        if form.is_valid(): # 对数据进行校验

            data = form.cleaned_data
            print("data:",data)
            # del data['password2']

            user = form.save(commit=False)     # 保存数据
            #设置密码
            user.set_password(data['password'])
            user.save()

            return HttpResponse("ok")
        else:
            errors = form.errors
            print(errors)
            return render(request,'myaccount/register.html',locals())


def user_logout(request):
    """
    用户退出
    :param request:
    :return:
    """
    logout(request)
    return redirect(reverse('index'))


def reset_pwd(request):
    """
    重置密码
    :param request:
    :return:
    """
    reset_username = request.POST.get("username")
    reset_password = request.POST.get("password")
    user = User.objects.get(username=reset_username)
    user.set_password(reset_password)
    user.save()

@login_required()
def user_delete(request,id):
    print(request.GET)
    print(request.POST)
    print("id------:",id)
    print("method:-------",request.method)
    if request.method == 'POST':
        user = User.objects.get(id=id)
        if request.user == user:
            logout(request)
            user.delete()
            return redirect(reverse("article:article_list"))
        else:
            return HttpResponse("您没有操作的权限!")
    else:
        return HttpResponse("非法操作")

'''








